#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = (1 << 17) + 5;

int n, m, nn = 1;
int aa[N], c[N], lg[N], mx[20][N << 1];
int a[N], ok[N];
bool vis[N];
ll ans[N], sum;
struct Node {
  bool d;
  bool f;
  int id;
  ll sum;
} tr[N << 1];

// 初始化公用部分数据
void build(int p, int l, int r, int mx[]) {
  tr[p].sum = (l + r) * (r + 1ll - l) / 2;
  if (l == r) return;
  int mid = (l + r) >> 1, k = lg[r + 1 - l], d = tr[p].d;
  if (~mx[p])
    mx[p << 1] = mx[p << 1 | 1] = mx[p];
  else
    mx[p << 1 | d] = k;
  build(p << 1, l, mid, mx);
  build(p << 1 | 1, mid + 1, r, mx);
}
void build2(int p, int l, int r) {
  if (l == r) {
    tr[p].id = l;
    return;
  }
  int mid = (l + r) >> 1, k = lg[r + 1 - l], d = tr[p].d;
  build2(p << 1, l, mid);
  build2(p << 1 | 1, mid + 1, r);
  bool win = a[tr[p << 1 | d].id] >= k;
  tr[p].id = tr[p << 1 | (d ^ !win)].id;
  if (!d && win)
    tr[p].f = 1, ok[r]++, ok[mid]--;
  else
    tr[p].f = 0;
}
void add(int id) {
  sum += !vis[id] * id;
  vis[id] = 1;
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  rep(i, 1, n) cin >> aa[i];
  rep(i, 1, m) cin >> c[i];
  while (nn < n) nn <<= 1;
  for (int i = nn / 2; i; i >>= 1) {
    for (int j = i; j < i * 2; j++) {
      char c;
      cin >> c;
      tr[j].d = c - '0';
    }
  }
  rep(i, 2, nn) lg[i] = lg[i >> 1] + 1;
  memset(mx, -1, sizeof(mx));
  for (int s = 0; (1 << s) <= nn; s++) build(1 << s, 1, nn >> s, mx[s]);

  int T;
  cin >> T;
  while (T--) {
    int x[4];
    rep(i, 0, 3) cin >> x[i];
    rep(i, 1, n) a[i] = aa[i] ^ x[i & 3];
    memset(ok, 0, sizeof(ok));
    build2(1, 1, nn);
    for (int i = nn, s = 0, rt = 1; i; i--) {
      if ((1 << lg[i]) == i) {
        if (i != nn) s++, rt <<= 1;
        sum = 0;
        rep(j, 1, i) vis[j] = 0;
        add(tr[rt].id);
      }
      ans[i] = sum;
      if (ok[i] += ok[i + 1]) continue;
      add(i);
      int p = nn + i - 1;
      while (p != rt) {
        int ch = p & 1;
        p >>= 1;
        if (!ch) {
          if (tr[p].f)
            sum += tr[p << 1 | 1].sum;
          else
            break;
        } else if (a[tr[p << 1].id] >= mx[s][p << 1])
          add(tr[p << 1].id);
      }
    }
    ll res = 0;
    rep(i, 1, m) res ^= i * ans[c[i]];
    cout << res << endl;
  }
  return 0;
}